{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## 2.1 常用数据对象"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:35:23.174758Z",
     "end_time": "2024-05-07T21:35:23.616685Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 1.Series对象"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "shanghai     27466.15\nbeijing      24899.30\nguangzhou    19610.90\nshenzhen     19492.40\ntianjin      17885.39\nchongqing    17558.76\nsuzhou       15475.09\nchengdu      12170.20\ndtype: float64"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = np.array([27466.15, 24899.3, 19610.9, 19492.4, 17885.39, 17558.76, 15475.09, 12170.2])\n",
    "gdp = pd.Series(g, index=['shanghai', 'beijing', 'guangzhou', 'shenzhen', 'tianjin', 'chongqing', 'suzhou', 'chengdu'])\n",
    "gdp"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:35:23.621917Z",
     "end_time": "2024-05-07T21:35:23.651954Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "0    27466.15\n1    24899.30\n2    19610.90\n3    19492.40\n4    17885.39\n5    17558.76\n6    15475.09\n7    12170.20\ndtype: float64"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g2 = pd.Series(g)\n",
    "g2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:35:23.644618Z",
     "end_time": "2024-05-07T21:35:23.651954Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "0    100\n1    200\n2    300\ndtype: int64"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Series(data=[100, 200, 300])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:35:23.651954Z",
     "end_time": "2024-05-07T21:35:23.672765Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "a    100\nb    100\nc    100\ndtype: int64"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Series(100, index=['a', 'b', 'c'])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:35:23.668655Z",
     "end_time": "2024-05-07T21:35:23.705854Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "1    0.1\n2    0.2\n3    0.3\n4    0.4\ndtype: float64"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = pd.Series([0.1, 0.2, 0.3, 0.4], index=[1, 2, 3, 4])\n",
    "d"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:35:23.692501Z",
     "end_time": "2024-05-07T21:35:23.705854Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "0.1"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d[1]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:35:23.705314Z",
     "end_time": "2024-05-07T21:35:23.720450Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "wuhan       11912.6\nhangzhou    11050.5\nnanjing     10503.0\ndtype: float64"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gdp2 = pd.Series({\"wuhan\": 11912.6, \"hangzhou\": 11050.5, \"nanjing\": 10503})\n",
    "gdp2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:35:23.720450Z",
     "end_time": "2024-05-07T21:35:23.774327Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "nanjing     10503.0\nwuhan       11912.6\nhangzhou    11050.5\ndtype: float64"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gdp3 = pd.Series({\"wuhan\": 11912.6, \"hangzhou\": 11050.5, \"nanjing\": 10503}, index=['nanjing', 'wuhan', 'hangzhou'])\n",
    "gdp3"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:35:23.747565Z",
     "end_time": "2024-05-07T21:35:23.781719Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "Index(['shanghai', 'beijing', 'guangzhou', 'shenzhen', 'tianjin', 'chongqing',\n       'suzhou', 'chengdu'],\n      dtype='object')"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gdp.index"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:35:23.758624Z",
     "end_time": "2024-05-07T21:35:23.781719Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "array([27466.15, 24899.3 , 19610.9 , 19492.4 , 17885.39, 17558.76,\n       15475.09, 12170.2 ])"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gdp.values"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:35:23.778011Z",
     "end_time": "2024-05-07T21:35:23.822476Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [],
   "source": [
    "gdp.index = [\"SHANGHAI\", \"BEIJING\", \"GUANGZHOU\", \"SHENZHEN\", \"TIANJIN\", \" CHONGQING\", \"SUZHOU\", \"CHENGDU\"]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:35:23.802660Z",
     "end_time": "2024-05-07T21:35:23.850932Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "data": {
      "text/plain": "SHANGHAI      27466.15\nBEIJING       24899.30\nGUANGZHOU     19610.90\nSHENZHEN      19492.40\nTIANJIN       17885.39\n CHONGQING    17558.76\nSUZHOU        15475.09\nCHENGDU       12170.20\nName: GDP(hunder million RMB), dtype: float64"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gdp.name = \"GDP(hunder million RMB)\"\n",
    "gdp"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:35:23.816908Z",
     "end_time": "2024-05-07T21:35:23.855645Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "City Name\nSHANGHAI      27466.15\nBEIJING       24899.30\nGUANGZHOU     19610.90\nSHENZHEN      19492.40\nTIANJIN       17885.39\n CHONGQING    17558.76\nSUZHOU        15475.09\nCHENGDU       12170.20\nName: GDP(hunder million RMB), dtype: float64"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gdp.index.name = \"City Name\"\n",
    "gdp"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:35:23.835132Z",
     "end_time": "2024-05-07T21:35:23.910374Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "pandas.core.indexes.base.Index"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(gdp.index)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:35:23.855645Z",
     "end_time": "2024-05-07T21:35:23.920999Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.DataFrame对象"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "data": {
      "text/plain": "          0        1\n0  27466.15  2419.70\n1  24899.30  2172.90\n2  19610.90  1350.11\n3  19492.60  1137.87\n4  17885.39  1562.12\n5  17558.76  3016.55\n6  15475.09  1375.00\n7  12170.20  1591.76",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>0</th>\n      <th>1</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>27466.15</td>\n      <td>2419.70</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>24899.30</td>\n      <td>2172.90</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>19610.90</td>\n      <td>1350.11</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>19492.60</td>\n      <td>1137.87</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>17885.39</td>\n      <td>1562.12</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>17558.76</td>\n      <td>3016.55</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>15475.09</td>\n      <td>1375.00</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>12170.20</td>\n      <td>1591.76</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gp = pd.DataFrame(\n",
    "    [[27466.15, 2419.70], [24899.30, 2172.90], [19610.90, 1350.11], [19492.60, 1137.87], [17885.39, 1562.12],\n",
    "     [17558.76, 3016.55], [15475.09, 1375.00], [12170.20, 1591.76]])\n",
    "gp"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:35:23.872981Z",
     "end_time": "2024-05-07T21:35:23.920999Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "data": {
      "text/plain": "                GDP  Population\nSHANGHAI   27466.15     2419.70\nBEIJING    24899.30     2172.90\nGUANGZHOU  19610.90     1350.11\nSHENZHEN   19492.60     1137.87\nTIANJIN    17885.39     1562.12\nCHONGQING  17558.76     3016.55\nSUZHOU     15475.09     1375.00\nCHENGDU    12170.20     1591.76",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>GDP</th>\n      <th>Population</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>SHANGHAI</th>\n      <td>27466.15</td>\n      <td>2419.70</td>\n    </tr>\n    <tr>\n      <th>BEIJING</th>\n      <td>24899.30</td>\n      <td>2172.90</td>\n    </tr>\n    <tr>\n      <th>GUANGZHOU</th>\n      <td>19610.90</td>\n      <td>1350.11</td>\n    </tr>\n    <tr>\n      <th>SHENZHEN</th>\n      <td>19492.60</td>\n      <td>1137.87</td>\n    </tr>\n    <tr>\n      <th>TIANJIN</th>\n      <td>17885.39</td>\n      <td>1562.12</td>\n    </tr>\n    <tr>\n      <th>CHONGQING</th>\n      <td>17558.76</td>\n      <td>3016.55</td>\n    </tr>\n    <tr>\n      <th>SUZHOU</th>\n      <td>15475.09</td>\n      <td>1375.00</td>\n    </tr>\n    <tr>\n      <th>CHENGDU</th>\n      <td>12170.20</td>\n      <td>1591.76</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gp.index = ['SHANGHAI', 'BEIJING', 'GUANGZHOU', 'SHENZHEN', 'TIANJIN',\n",
    "            'CHONGQING', 'SUZHOU', 'CHENGDU']\n",
    "gp.columns = ['GDP', 'Population']\n",
    "gp"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:35:23.891206Z",
     "end_time": "2024-05-07T21:35:23.920999Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "data": {
      "text/plain": "Items           GDP  Population\nCity_Name                      \nSHANGHAI   27466.15     2419.70\nBEIJING    24899.30     2172.90\nGUANGZHOU  19610.90     1350.11\nSHENZHEN   19492.60     1137.87\nTIANJIN    17885.39     1562.12\nCHONGQING  17558.76     3016.55\nSUZHOU     15475.09     1375.00\nCHENGDU    12170.20     1591.76",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>Items</th>\n      <th>GDP</th>\n      <th>Population</th>\n    </tr>\n    <tr>\n      <th>City_Name</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>SHANGHAI</th>\n      <td>27466.15</td>\n      <td>2419.70</td>\n    </tr>\n    <tr>\n      <th>BEIJING</th>\n      <td>24899.30</td>\n      <td>2172.90</td>\n    </tr>\n    <tr>\n      <th>GUANGZHOU</th>\n      <td>19610.90</td>\n      <td>1350.11</td>\n    </tr>\n    <tr>\n      <th>SHENZHEN</th>\n      <td>19492.60</td>\n      <td>1137.87</td>\n    </tr>\n    <tr>\n      <th>TIANJIN</th>\n      <td>17885.39</td>\n      <td>1562.12</td>\n    </tr>\n    <tr>\n      <th>CHONGQING</th>\n      <td>17558.76</td>\n      <td>3016.55</td>\n    </tr>\n    <tr>\n      <th>SUZHOU</th>\n      <td>15475.09</td>\n      <td>1375.00</td>\n    </tr>\n    <tr>\n      <th>CHENGDU</th>\n      <td>12170.20</td>\n      <td>1591.76</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gp = pd.DataFrame([[27466.15, 2419.70], [24899.30, 2172.90], [19610.90, 1350.11],\n",
    "                   [19492.60, 1137.87], [17885.39, 1562.12], [17558.76, 3016.55], [15475.09, 1375.00],\n",
    "                   [12170.20, 1591.76]],\n",
    "                  index=['SHANGHAI', 'BEIJING', 'GUANGZHOU', 'SHENZHEN', 'TIANJIN', 'CHONGQING',\n",
    "                         'SUZHOU', 'CHENGDU'],\n",
    "                  columns=['GDP', 'Population'])\n",
    "gp.index.name = 'City_Name'\n",
    "gp.columns.name = \"Items\"\n",
    "gp"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:35:23.910374Z",
     "end_time": "2024-05-07T21:35:23.974682Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "data": {
      "text/plain": "              city   marks\nPKU        beijing  100.00\nTsinghua   beijing   96.91\nWHU          hubei   82.57\nFudan     shanghai   82.47",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>city</th>\n      <th>marks</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>PKU</th>\n      <td>beijing</td>\n      <td>100.00</td>\n    </tr>\n    <tr>\n      <th>Tsinghua</th>\n      <td>beijing</td>\n      <td>96.91</td>\n    </tr>\n    <tr>\n      <th>WHU</th>\n      <td>hubei</td>\n      <td>82.57</td>\n    </tr>\n    <tr>\n      <th>Fudan</th>\n      <td>shanghai</td>\n      <td>82.47</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.DataFrame({\"city\": [\"beijing\", \"beijing\", \"hubei\", \"shanghai\"],\n",
    "              \"marks\": [100.00, 96.91, 82.57, 82.47]},\n",
    "             index=[\"PKU\", \"Tsinghua\", \"WHU\", \"Fudan\"])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:35:23.942223Z",
     "end_time": "2024-05-07T21:35:24.067279Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "data": {
      "text/plain": "        GDP  Population\n0  27466.15     2419.70\n1  24899.30     2172.90\n2  19610.90     1350.11\n3  19492.60     1137.87",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>GDP</th>\n      <th>Population</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>27466.15</td>\n      <td>2419.70</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>24899.30</td>\n      <td>2172.90</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>19610.90</td>\n      <td>1350.11</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>19492.60</td>\n      <td>1137.87</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict_gdp = {\"GDP\": [27466.15, 24899.30, 19610.90, 19492.60],\n",
    "            \"Population\": [2419.70, 2172.90, 1350.11, 1137.87]}\n",
    "pd.DataFrame.from_dict(dict_gdp)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:35:23.958893Z",
     "end_time": "2024-05-07T21:35:24.071733Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "data": {
      "text/plain": "                   0        1         2         3\nGDP         27466.15  24899.3  19610.90  19492.60\nPopulation   2419.70   2172.9   1350.11   1137.87",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>0</th>\n      <th>1</th>\n      <th>2</th>\n      <th>3</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>GDP</th>\n      <td>27466.15</td>\n      <td>24899.3</td>\n      <td>19610.90</td>\n      <td>19492.60</td>\n    </tr>\n    <tr>\n      <th>Population</th>\n      <td>2419.70</td>\n      <td>2172.9</td>\n      <td>1350.11</td>\n      <td>1137.87</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.DataFrame.from_dict(dict_gdp, orient=\"index\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:35:23.975200Z",
     "end_time": "2024-05-07T21:35:24.114069Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "data": {
      "text/plain": "                GDP  Population\nSHANGHAI   27466.15     2419.70\nBEIJING    24899.30     2172.90\nGUANGZHOU  19610.90     1350.11\nSHENZHEN   19492.60     1137.87",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>GDP</th>\n      <th>Population</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>SHANGHAI</th>\n      <td>27466.15</td>\n      <td>2419.70</td>\n    </tr>\n    <tr>\n      <th>BEIJING</th>\n      <td>24899.30</td>\n      <td>2172.90</td>\n    </tr>\n    <tr>\n      <th>GUANGZHOU</th>\n      <td>19610.90</td>\n      <td>1350.11</td>\n    </tr>\n    <tr>\n      <th>SHENZHEN</th>\n      <td>19492.60</td>\n      <td>1137.87</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict_gdp2 = {\"GDP\": {\"SHANGHAI\": 27466.15, \"BEIJING\": 24899.30,\n",
    "                     \"GUANGZHOU\": 19610.90, \"SHENZHEN\": 19492.60},\n",
    "             \"Population\": {\"SHANGHAI\": 2419.70, \"BEIJING\": 2172.90,\n",
    "                            \"GUANGZHOU\": 1350.11, \"SHENZHEN\": 1137.87}}\n",
    "pd.DataFrame.from_dict(dict_gdp2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:35:24.005502Z",
     "end_time": "2024-05-07T21:35:24.135675Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "data": {
      "text/plain": "            SHANGHAI  BEIJING  GUANGZHOU  SHENZHEN\nGDP         27466.15  24899.3   19610.90  19492.60\nPopulation   2419.70   2172.9    1350.11   1137.87",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>SHANGHAI</th>\n      <th>BEIJING</th>\n      <th>GUANGZHOU</th>\n      <th>SHENZHEN</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>GDP</th>\n      <td>27466.15</td>\n      <td>24899.3</td>\n      <td>19610.90</td>\n      <td>19492.60</td>\n    </tr>\n    <tr>\n      <th>Population</th>\n      <td>2419.70</td>\n      <td>2172.9</td>\n      <td>1350.11</td>\n      <td>1137.87</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.DataFrame.from_dict(dict_gdp2, orient=\"index\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:35:24.026118Z",
     "end_time": "2024-05-07T21:35:24.149298Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "data": {
      "text/plain": "dict_items([('GDP', [27466.15, 24899.3, 19610.9, 19492.6]), ('Population', [2419.7, 2172.9, 1350.11, 1137.87])])"
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "items_gdp = dict_gdp.items()\n",
    "items_gdp"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:35:24.051370Z",
     "end_time": "2024-05-07T21:35:24.175358Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "data": {
      "text/plain": "Items           GDP  Population\nCity_Name                      \nSHANGHAI   27466.15     2419.70\nBEIJING    24899.30     2172.90\nGUANGZHOU  19610.90     1350.11\nSHENZHEN   19492.60     1137.87\nTIANJIN    17885.39     1562.12\nCHONGQING  17558.76     3016.55\nSUZHOU     15475.09     1375.00\nCHENGDU    12170.20     1591.76",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>Items</th>\n      <th>GDP</th>\n      <th>Population</th>\n    </tr>\n    <tr>\n      <th>City_Name</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>SHANGHAI</th>\n      <td>27466.15</td>\n      <td>2419.70</td>\n    </tr>\n    <tr>\n      <th>BEIJING</th>\n      <td>24899.30</td>\n      <td>2172.90</td>\n    </tr>\n    <tr>\n      <th>GUANGZHOU</th>\n      <td>19610.90</td>\n      <td>1350.11</td>\n    </tr>\n    <tr>\n      <th>SHENZHEN</th>\n      <td>19492.60</td>\n      <td>1137.87</td>\n    </tr>\n    <tr>\n      <th>TIANJIN</th>\n      <td>17885.39</td>\n      <td>1562.12</td>\n    </tr>\n    <tr>\n      <th>CHONGQING</th>\n      <td>17558.76</td>\n      <td>3016.55</td>\n    </tr>\n    <tr>\n      <th>SUZHOU</th>\n      <td>15475.09</td>\n      <td>1375.00</td>\n    </tr>\n    <tr>\n      <th>CHENGDU</th>\n      <td>12170.20</td>\n      <td>1591.76</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gp"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:35:24.071733Z",
     "end_time": "2024-05-07T21:35:24.252616Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [],
   "source": [
    "gp.to_csv(\"./gp.csv\",\n",
    "          columns=[\"GDP\", \"Population\"],\n",
    "          index_label=[\"SHANGHAI\", \"BEIJING\", \"GUANGZHOU\", \"SHENZHEN\",\n",
    "                       \"TIANJIN\", \"CHONGQING\", \"SUZHOU\", \"CHENGDU\"],\n",
    "          header=False)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:35:24.098130Z",
     "end_time": "2024-05-07T21:35:24.253665Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [],
   "source": [
    "pd.DataFrame.to_csv(gp, \"./gp2.csv\",\n",
    "                    columns=[\"GDP\", \"Population\"],\n",
    "                    index_label=[\"SHANGHAI\", \"BEIJING\", \"GUANGZHOU\", \"SHENZHEN\",\n",
    "                                 \"TIANJIN\", \"CHONGQING\", \"SUZHOU\", \"CHENGDU\"],\n",
    "                    header=False)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:35:24.119370Z",
     "end_time": "2024-05-07T21:35:24.253665Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:35:24.135675Z",
     "end_time": "2024-05-07T21:35:24.275960Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
